import store from '@/store'
import md5 from 'js-md5'
import { jsonp } from 'vue-jsonp'

const sign = {
  translate (q, from, to) {
    let data = store.getters.getBaiduTan
    let salt = new Date().getTime()
    let signData = data.appID + q + salt + data.secretKey
    console.log(signData)
    let sign = md5(signData)

    return new Promise(resolve => {
      jsonp(store.getters.getBaiduTan.tranAPI,{
        q: q,
        from: from,
        to: to,
        appid: store.getters.getBaiduTan.appID,
        salt: salt,
        sign: sign
      }).then(res => {
        resolve(res)
        return res.data
      }).catch(err => {
        alert('翻译出错')
      })

      // axios.get(store.getters.getBaiduTan.tranAPI,{
      //   params:{
      //     q: q,
      //     from: from,
      //     to: to,
      //     appid: store.getters.getBaiduTan.appID,
      //     salt: salt,
      //     sign: sign
      //   },
      //   headers:{
      //     "Content-Type": "application/octet-stream",
      //     'Access-Control-Allow-Origin':'*'
      //   }
      // }).then(res => {
      //   resolve(res)
      //   return res.data
      // }).catch(err => {
      //   alert('翻译出错')
      // })


      // apiFun.sign({
      //   q: q,
      //   from: from,
      //   to: to,
      //   appid: store.getters.getBaiduTan.appID,
      //   salt: salt,
      //   sign: sign
      // }).then(res => {
      //   resolve(res)
      //   return res.data
      // }).catch(err => {
      //   alert('翻译出错')
      // })
    })

  }
}

export default sign
